//
//  CyclopediaCtrl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/24.
//  Copyright © 2015年 game. All rights reserved.
//

#import "CyclopediaCtrl.h"
#import "AticleheadCell.h"
#import "CommentCell.h"
#import "UIImageView+WebCache.h"
#import "AticHeadView.h"
#import "AticleFootView.h"
#import "CharacterCell.h"
@interface CyclopediaCtrl ()<UITableViewDataSource,UITableViewDelegate>
{

    
    UITableView *_tableview ;
    
    CGFloat _aticleHeadHight;//文章详情的高度；
    
    CGFloat _comentHight;//评论列表的高度
    
    
    
    AticleFootView *_footview;
    
    BOOL keybIsopen;
    
    CGFloat  _CharCellHight;
    
    UIView *_sectionView;
    
    UIView *_secLinView;//相关词条+我要提问 滑条
    
    BOOL  isClick;
    
    UIScrollView *_footScrollview;
}
@end

@implementation CyclopediaCtrl

-(void)viewWillAppear:(BOOL)animated{
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    isClick = YES;
    self.view.backgroundColor = [UIColor yellowColor];
    [self CreactTableview];
    [self.view addSubview:[self NavGationView]];
//    [self PublishView];
}

#pragma mark--导航栏
-(UIView *)NavGationView{
    
    AticHeadView *headview = [[AticHeadView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    headview.backgroundColor = [UIColor whiteColor];
    headview.titile.text = @"文章详情";
    [headview.BackBnt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [headview.shareBnt addTarget:self action:@selector(Share:) forControlEvents:UIControlEventTouchUpInside];
    [headview.collectBnt addTarget:self action:@selector(Collect:) forControlEvents:UIControlEventTouchUpInside];
    return headview;
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark---分享操作
-(void)Share:(UIButton *)sender{
    
}
#pragma mark---收藏操作
-(void)Collect:(UIButton *)sender{
    
    
}


#pragma mark--底部发表View
-(void)PublishView{
    
    _footview = [[AticleFootView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50)];
    _footview.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_footview];
}

/*是否隐藏导航栏*/
-(BOOL)prefersStatusBarHidden{
    
    if (keybIsopen) {
        return YES;
    }else{
        return NO;
        
    }
    
}


#pragma mark---tableview
-(void)CreactTableview{
    _tableview = [self CreatTableview:_tableview WhithRect:CGRectMake(0,44, SCREEN_WIDTH, SCREEN_HEIGHT) AndStyle:UITableViewStylePlain];
    _tableview.dataSource = self;
    _tableview.delegate = self;
    [_tableview registerClass:[AticleheadCell class] forCellReuseIdentifier:@"AticleCell"];
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableview registerClass:[CharacterCell class] forCellReuseIdentifier:@"CharacterCell"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0) {
        return 1;
    }else{
        return 0;
    
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    /*一共分两组，文章，相关词条+我要提问*/
    return 2;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    /*最后一组尾部高度为0，其他为15*/
    if (section==0) {
        return 15;
    }else{
        
        return 150;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
        return _aticleHeadHight+105;
    
    }else{
        
        return  0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    /*头部高度，第0组为0，用于显示 相关文章，钓友评论  标题*/
    if (section==0) {
        return 0;
    }else{
        
        return 50;
    }
}

#pragma mark--相关词条+我要提问View
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    
    NSArray *arry = @[@"相关词条",@"我要提问"];
    CGFloat xd = SCREEN_WIDTH*0.1;
    CGFloat w = (SCREEN_WIDTH-xd*4)/arry.count;
    
    for (int i=0;i<arry.count; i++) {
        
       UIButton *bnt =  [self CreatBntAddView:view WhithRect:CGRectMake(xd+w*i+xd*(i*2), 0,w, 50) AndType:UIButtonTypeSystem AndBntImgName:@"er" AndTitle:arry[i] Font:18];
        
        [bnt addTarget:self action:@selector(SelectClick:) forControlEvents:UIControlEventTouchUpInside];
        bnt.backgroundColor= [UIColor whiteColor];
       
        bnt.tag = i+1;
        
        if (bnt.tag==1) {
            [bnt setTitleColor:mainColor forState:UIControlStateNormal];
        }else{
            [bnt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        }

     }
    
    [self Linview:CGRectMake(W(view)/2, 8, 1, H(view)-16) BackgroundColor:RGBA(230, 230, 230, 1) AddView:view];
    [self Linview:CGRectMake(0,H(view)-1,SCREEN_WIDTH, 1) BackgroundColor:RGBA(230, 230, 230, 1) AddView:view];
    _secLinView = [self Linview:CGRectMake(xd, H(view)-2, w, 2) BackgroundColor:mainColor AddView:view];
    view.backgroundColor = [UIColor whiteColor];
    _sectionView = view;
    return view;
}
-(void)SelectClick:(UIButton *)click{

    if (click.tag==1) {
        isClick=YES;
    }else {
    isClick = !isClick;
    
    }

    /*拿到当前视图在父视图的位置 通过 index*/
    /*左边按钮的index=0，右边的index==1 与Bnt的tag值相差1*/
    for (int i=0; i<_sectionView.subviews.count; i++) {
        
        UIButton *bnt = [_sectionView.subviews objectAtIndex:i];
        if (bnt.tag == click.tag) {
            [bnt setTitleColor:mainColor forState:UIControlStateNormal];
        }else if(bnt.tag==1||bnt.tag==2){
            [bnt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }else{
            break;
        }
    }
    
    /*动画*/
    [UIView animateWithDuration:0.2 animations:^{
        
      /*改变滑条坐标*/
        CGRect rect = _secLinView.frame;
        rect.origin.x = click.frame.origin.x;
        _secLinView.frame = rect;

     /*实现与 Scrollview 关联*/
    if (click.tag==1) {
        _footScrollview.contentOffset = CGPointMake(0, 0);
    }else{
        _footScrollview.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
      }
    }];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section==1) {
        
        _footScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,150)];
        _footScrollview.contentSize = CGSizeMake(SCREEN_WIDTH*2, H(_footScrollview));
        _footScrollview.bounces = NO;
        _footScrollview.showsHorizontalScrollIndicator = NO;
        _footScrollview.pagingEnabled = YES;
        _footScrollview.scrollEnabled  = NO;
        _footScrollview.contentOffset =CGPointMake(0, 0);
        [self EntryView];
        [self QuizView];
        return _footScrollview;
        
    }else{
        return nil;
    
    }
}
#pragma mark--相关词条
-(void)EntryView{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, H(_footScrollview))];
    
    CharacterCell *cell = [[CharacterCell alloc]init];
 
    cell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150);
    [view addSubview:cell];
    _CharCellHight = cell.cellHight;
    
    cell.ButtionBlock = ^(UIButton *sender){

        };

    [_footScrollview addSubview:view];

}
#pragma mark--我要提问
-(void)QuizView{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, H(_footScrollview))];
    
    view.backgroundColor = [UIColor greenColor];
    [_footScrollview addSubview:view];

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.section==0) {
    
        AticleheadCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[AticleheadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AticleCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.title.text = @"男子清理污水抓到10吨大鱼 长达12米";
        cell.Sources.text = [NSString stringWithFormat:@"来源：%@",@"钓鱼人"];
        [cell.time setTitle:@"2015-12-12" forState:UIControlStateNormal];

    
        [cell.pramisNumb setTitle:@"2980" forState:UIControlStateNormal];
        
        /*计算字体高度*/
//        CGSize size = CGSizeMake(SCREEN_WIDTH-20, 0);
//        CGRect titTleRect = [self CountTextWhithSize:size AndString:cell.title.text AndfFontSize:16];

    
        return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

-(void)didReceiveMemoryWarning{
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    
    if ([self.view window] == nil) {
        
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        
    }
}


@end
